<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/all.conf.php');
require_once(__PHY_WEB.__DIR_ADM.'/admin.inc.php');
require_once(__PHY_LIB.'/class/aFunc.class.php');

$rReq = validate($_REQUEST, 'action:u|id:n|func_ename:u|func_cname:u|group_cname:u|description:u');
$sMsg = '';
$sAction = (in_array($rReq['action'], array('list', 'edit', 'new', 'add', 'update')) ? $rReq['action'] : 'list');
if ('add' == $sAction) {
	$oFunc = new aFunc();
	$oFunc->set('func_ename', $rReq['func_ename']);
	$oFunc->set('func_cname', $rReq['func_cname']);
	$oFunc->set('group_cname', $rReq['group_cname']);
	$oFunc->set('description', $rReq['description']);
	$oFunc->set('add_user_id', $_SESSION['ADM_USER_ID']);
	$oFunc->set('add_time', date('Y-m-d H:i:s'));
	if ($s = $oFunc->invalid()) {
		$sMsg .= "参数不规范: {$s}".$_br;
	}
	elseif ($oFunc->insert()) {
		$_ME->setAccess($oFunc->getID(), 'READ');
		$sMsg .= '添加成功。你对其有读权限了。'.$_br;
	}
	else {
		$sMsg .= '添加失败'.$_br;
	}
	$sAction = 'list';
}
if ('update' == $sAction) {
	$oFunc = new aFunc($rReq['id']);
	$oFunc->set('func_cname', $rReq['func_cname']);
	$oFunc->set('group_cname', $rReq['group_cname']);
	$oFunc->set('description', $rReq['description']);
	if ($s = $oFunc->invalid()) {
		$sMsg .= "参数不规范: {$s}".$_br;
	}
	elseif ($oFunc->update()) {
		$sMsg .= '修改成功'.$_br;
	}
	else {
		$sMsg .= '修改失败'.$_br;
	}
	$sAction = 'list';
}
if ('edit' == $sAction) {
	$oFunc = new aFunc($rReq['id']);
	$oTpl->assign('rFunc', $oFunc->getData());
}
if ('list' == $sAction) {
	$oAdmFuncList = new aFuncList();
	$oAdmFuncList->cfgOrder("group_cname,func_cname");
	$oAdmFuncList->load();
	$oTpl->assign('rFuncs', $oAdmFuncList->fetchArray());
}

$oTpl->assign('sMsg', $sMsg);
$oTpl->assign('sAction', $sAction);
$oTpl->display(dirname(__FILE__).'/'.basename(__FILE__, '.php').'.tpl');
?>
